package com.stylefeng.guns.rest.modular.film.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.film.*;
import com.stylefeng.guns.api.util.DateUtil;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FilmServiceImpl
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/9/13  20:47
 * @Version 1.0
 */
@Component
@Service(interfaceClass = FilmServiceApi.class)
public class FilmServiceImpl implements FilmServiceApi {

    @Autowired
    private MoocBannerTMapper moocBannerTMapper;

    @Autowired
    private MoocFilmTMapper moocFilmTMapper;

    @Autowired
    private MoocCatDictTMapper moocCatDictTMapper;

    @Autowired
    private MoocSourceDictTMapper moocSourceDictTMapper;

    @Autowired
    private MoocYearDictTMapper moocYearDictTMapper;

    @Autowired
    private MoocFilmInfoTMapper moocFilmInfoTMapper;

    @Autowired
    private MoocActorTMapper moocActorTMapper;


    @Override
    public List<BannerVo> getBanners() {
        List<MoocBannerT> moocBannerTS = moocBannerTMapper.selectList(null);

        List<BannerVo> bannerVos = new ArrayList<>();
        bannerVos.clear();
        for (MoocBannerT bannerT : moocBannerTS) {
            BannerVo bannerVo = new BannerVo();
            bannerVo.setBannerId(bannerT.getUuid() + "")
                    .setBannerAddress(bannerT.getBannerAddress())
                    .setBannerUrl(bannerT.getBannerUrl());
            bannerVos.add(bannerVo);
        }

        return bannerVos;
    }

    /**
     * 热映接口
     *
     * @param isLimit
     * @param nums
     * @param nowPage
     * @param sortId
     * @param sourceId
     * @param yearId
     * @param catId
     * @return
     */
    @Override
    public FilmsVo getHotFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmsVo filmsVo = new FilmsVo();
        List<FilmInfo> filmInfos = new ArrayList<>();
        filmInfos.clear();

        //判断是否是 首页需要的内容
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 1);
        if (isLimit) {
            //如果是则限制条数 页数从 1 开始
            Page<MoocFilmT> moocFilmTPage = new Page<>(1, nums);
            List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(moocFilmTPage, entityWrapper);

            //组装数据
            filmsVo.setFilmInfo(getFilmInfos(moocFilmTS));
            filmsVo.setFilmNum(moocFilmTS.size());
            //List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectList(entityWrapper);
        } else {
            //如果不是 则显示列表页

            Page<MoocFilmT> moocFilmTPage = null;
            // 1 --> 按照热门程度 2--> 按时间 3 --> 按评价
            switch (sourceId) {
                case 1:
                    moocFilmTPage = new Page<>(nowPage, nums, "film_box_office");
                    break;
                case 2:
                    moocFilmTPage = new Page<>(nowPage, nums, "film_time");
                    break;
                case 3:
                    moocFilmTPage = new Page<>(nowPage, nums, "film_score");
                    break;
                default:
                    moocFilmTPage = new Page<>(nowPage, nums, "film_box_office");
                    break;

            }


            //如果sourceId yearId catId 不为99
            if (sourceId != 99) {
                entityWrapper.eq("film_source", sourceId);
            }

            if (yearId != 99) {
                entityWrapper.eq("film_date", yearId);
            }

            if (catId != 99) {
                //数据库中的东西是 -->  #2#4#22#
                String catStr = "%#" + catId + "#%";
                entityWrapper.like("film_cats", catStr);

            }


            List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(moocFilmTPage, entityWrapper);

            //组装数据
            filmsVo.setFilmInfo(getFilmInfos(moocFilmTS));
            filmsVo.setFilmNum(moocFilmTS.size());
            //需要的总页数
            int totalCount = moocFilmTMapper.selectCount(entityWrapper);
            int totalPages = (totalCount / nums) + 1;

            filmsVo.setTotalPage(totalPages);
            filmsVo.setNowPage(nowPage);
        }
        return filmsVo;
    }

    /**
     * 即将上映
     *
     * @param isLimit
     * @param nums
     * @param nowPage
     * @param sortId
     * @param sourceId
     * @param yearId
     * @param catId
     * @return
     */
    @Override
    public FilmsVo getSoonFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmsVo filmsVo = new FilmsVo();
        List<FilmInfo> filmInfos = new ArrayList<>();
        filmInfos.clear();

        //判断是否是 首页需要的内容
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 2);
        if (isLimit) {
            //如果是则限制条数 页数从 1 开始
            Page<MoocFilmT> moocFilmTPage = new Page<>(1, nums);
            List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(moocFilmTPage, entityWrapper);

            //组装数据
            filmsVo.setFilmInfo(getFilmInfos(moocFilmTS));
            filmsVo.setFilmNum(moocFilmTS.size());
            //List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectList(entityWrapper);
        } else {
            //如果不是 则显示列表页
            Page<MoocFilmT> moocFilmTPage = null;
            // 1 --> 按照热门程度 2--> 按时间 3 --> 按评价
            switch (sourceId) {
                case 1:
                    moocFilmTPage = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;
                case 2:
                    moocFilmTPage = new Page<>(nowPage, nums, "film_time");
                    break;
                case 3:
                    moocFilmTPage = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;
                default:
                    moocFilmTPage = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;

            }
            //如果sourceId yearId catId 不为99
            if (sourceId != 99) {
                entityWrapper.eq("film_source", sourceId);
            }

            if (yearId != 99) {
                entityWrapper.eq("film_date", yearId);
            }

            if (catId != 99) {
                //数据库中的东西是 -->  #2#4#22#
                String catStr = "%#" + catId + "#%";
                entityWrapper.like("film_cats", catStr);

            }


            List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(moocFilmTPage, entityWrapper);

            //组装数据
            filmsVo.setFilmInfo(getFilmInfos(moocFilmTS));
            filmsVo.setFilmNum(moocFilmTS.size());
            //需要的总页数
            int totalCount = moocFilmTMapper.selectCount(entityWrapper);
            int totalPages = (totalCount / nums) + 1;

            filmsVo.setTotalPage(totalPages);
            filmsVo.setNowPage(nowPage);
        }
        return filmsVo;
    }

    /**
     * @param nums
     * @param nowPage
     * @param sortId
     * @param sourceId
     * @param yearId
     * @param catId
     * @return
     */
    @Override
    public FilmsVo getClassicFilms(int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmsVo filmsVo = new FilmsVo();
        List<FilmInfo> filmInfos = new ArrayList<>();
        filmInfos.clear();

        //判断是否是 首页需要的内容
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 3);

        Page<MoocFilmT> moocFilmTPage = null;
        // 1 --> 按照热门程度 2--> 按时间 3 --> 按评价
        switch (sourceId) {
            case 1:
                moocFilmTPage = new Page<>(nowPage, nums, "film_box_office");
                break;
            case 2:
                moocFilmTPage = new Page<>(nowPage, nums, "film_time");
                break;
            case 3:
                moocFilmTPage = new Page<>(nowPage, nums, "film_score");
                break;
            default:
                moocFilmTPage = new Page<>(nowPage, nums, "film_box_office");
                break;

        }
        //如果sourceId yearId catId 不为99
        if (sourceId != 99) {
            entityWrapper.eq("film_source", sourceId);
        }

        if (yearId != 99) {
            entityWrapper.eq("film_date", yearId);
        }

        if (catId != 99) {
            //数据库中的东西是 -->  #2#4#22#
            String catStr = "%#" + catId + "#%";
            entityWrapper.like("film_cats", catStr);

        }


        List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(moocFilmTPage, entityWrapper);

        //组装数据
        filmsVo.setFilmInfo(getFilmInfos(moocFilmTS));
        filmsVo.setFilmNum(moocFilmTS.size());
        //需要的总页数
        int totalCount = moocFilmTMapper.selectCount(entityWrapper);
        int totalPages = (totalCount / nums) + 1;

        filmsVo.setTotalPage(totalPages);
        filmsVo.setNowPage(nowPage);


        return filmsVo;
    }

    /**
     * 票房电影 已经上映过的
     *
     * @return
     */
    @Override
    public List<FilmInfo> getBoxRanking() {
        //票房电影 已经上映过的  票房前10
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 1);
        List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(new Page<>(1, 10, "film_box_office"), entityWrapper);
        return getFilmInfos(moocFilmTS);
    }

    @Override
    public List<FilmInfo> getExpectRanking() {
        //票房电影 即将上映过的  预售前10
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 2);
        List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(new Page<>(1, 10, "film_preSaleNum"), entityWrapper);
        return getFilmInfos(moocFilmTS);
    }

    @Override
    public List<FilmInfo> getTop() {
        //条件 正在上映的 评分  票房前10
        EntityWrapper<MoocFilmT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", 1);
        List<MoocFilmT> moocFilmTS = moocFilmTMapper.selectPage(new Page<>(1, 10, "film_score"), entityWrapper);
        return getFilmInfos(moocFilmTS);
    }

    //====== 获取影片条件接口

    @Override
    public List<CatVo> getCats() {
        //查询实体对象 就是 对应数据库的 MoocCatDictT
        List<MoocCatDictT> moocCats = moocCatDictTMapper.selectList(null);

        //将实体对象转换为业务对象  CatVo
        List<CatVo> catVos = new ArrayList<>();
        catVos.clear();
        for (MoocCatDictT moocCat : moocCats) {
            CatVo catVo = new CatVo();
            catVo.setCatId(moocCat.getUuid() + "")
                    .setCatName(moocCat.getShowName());
            catVos.add(catVo);
        }


        return catVos;
    }

    @Override
    public List<SourceVo> getSources() {
        List<MoocSourceDictT> moocSourceDictTS = moocSourceDictTMapper.selectList(null);

        List<SourceVo> sourceVos = new ArrayList<>();
        sourceVos.clear();
        for (MoocSourceDictT dictT : moocSourceDictTS) {
            SourceVo sourceVo = new SourceVo();
            sourceVo.setSourceId(dictT.getUuid() + "").setSourceName(dictT.getShowName());
            sourceVos.add(sourceVo);
        }
        return sourceVos;
    }

    @Override
    public List<YearVo> getYears() {
        //查询实体对象 就是 对应数据库的 MoocCatDictT
        List<MoocYearDictT> moocYearDictTS = moocYearDictTMapper.selectList(null);

        //将实体对象转换为业务对象  CatVo
        List<YearVo> yearVos = new ArrayList<>();
        yearVos.clear();
        for (MoocYearDictT dictT : moocYearDictTS) {
            YearVo yearVo = new YearVo();
            yearVo.setYearId(dictT.getUuid() + "").setYearName(dictT.getShowName());
            yearVos.add(yearVo);
        }

        return yearVos;
    }

    /**
     * 根据影片id或者名称获取  影片详情接口
     *
     * @param searchType
     * @param searchParam
     * @return
     */
    @Override
    public FilmDetailVo getFilmDetail(int searchType, String searchParam) {
        // searchType 1 - 按名称查找 2 - 按id查找
        FilmDetailVo detail = null;
        if (searchType == 1) {
            detail = moocFilmTMapper.getFilmDetailByName(searchParam);
        } else {
            detail = moocFilmTMapper.getFilmDetailById(searchParam);
        }
        return detail;
    }

    @Override
    public FilmDesVo getFilmDes(String filmId) {
        MoocFilmInfoT moocFilmInfoT = getMoocFilmInfoT(filmId);

        FilmDesVo filmDesVo = new FilmDesVo();
        filmDesVo.setBiography(moocFilmInfoT.getBiography())
                .setFilmId(moocFilmInfoT.getFilmId());
        return filmDesVo;
    }

    @Override
    public ImgVo getImgs(String filmId) {
        MoocFilmInfoT moocFilmInfoT = getMoocFilmInfoT(filmId);
        String[] filmImgs = moocFilmInfoT.getFilmImgs().split(",");

        ImgVo imgVo = new ImgVo();
        imgVo.setMainImg(filmImgs[0]);
        imgVo.setImg01(filmImgs[1]);
        imgVo.setImg02(filmImgs[2]);
        imgVo.setImg03(filmImgs[3]);
        imgVo.setImg04(filmImgs[4]);

//        for (int i = 1; i < filmImgs.length; i++) {
//
//        }
        return imgVo;
    }

    @Override
    public ActorVo getDectInfo(String filmId) {
        MoocFilmInfoT moocFilmInfoT = getMoocFilmInfoT(filmId);

        //获取导演
        Integer directorId = moocFilmInfoT.getDirectorId();

        MoocActorT moocActorT = moocActorTMapper.selectById(directorId);

        ActorVo actorVo = new ActorVo();
        actorVo.setDirectorName(moocActorT.getActorName())
               .setImgAddress(moocActorT.getActorImg());

        return actorVo;
    }

    @Override
    public List<ActorVo> getActors(String filmId) {
        //List<ActorVo> actorVos = new ArrayList<>();
        //actorVos.clear();

        List<ActorVo> actors = moocActorTMapper.getActors(filmId);


        return actors;
    }


    //处理数据的方法
    private MoocFilmInfoT getMoocFilmInfoT(String filmId) {
        MoocFilmInfoT moocFilmInfoT = new MoocFilmInfoT();
        moocFilmInfoT.setFilmId(filmId);

        moocFilmInfoT = moocFilmInfoTMapper.selectOne(moocFilmInfoT);

        return moocFilmInfoT;
    }


    //处理 数据的方法
    private List<FilmInfo> getFilmInfos(List<MoocFilmT> moocFilmTS) {
        List<FilmInfo> filmInfos = new ArrayList<>();
        filmInfos.clear();

        for (MoocFilmT filmT : moocFilmTS) {
            FilmInfo filmInfo = new FilmInfo();
            filmInfo.setBoxNum(filmT.getFilmBoxOffice())
                    .setScore(filmT.getFilmScore())
                    .setShowTime(DateUtil.getDay(filmT.getFilmTime()))
                    .setImgAddress(filmT.getImgAddress())
                    .setFilmType(filmT.getFilmType())
                    .setFilmScore(filmT.getFilmScore())
                    .setFilmName(filmT.getFilmName())
                    .setFilmId(filmT.getUuid() + "")
                    .setExpectNum(filmT.getFilmPresalenum());

            filmInfos.add(filmInfo);
        }

        return filmInfos;
    }


}
